package com.poisonh.unifiedauth.utils;

import com.google.gson.Gson;
import com.poisonh.unifiedauth.model.CertificationRecordModel;
import com.poisonh.unifiedauth.model.DeviceResult;
import com.poisonh.unifiedauth.model.DevicesModel;
import com.poisonh.unifiedauth.model.InfoCenterModel;
import com.poisonh.unifiedauth.model.InfoCenterResult;
import com.poisonh.unifiedauth.model.PassWdModel;
import com.poisonh.unifiedauth.model.PassWdResult;
import com.poisonh.unifiedauth.model.ProvinceModel;
import com.poisonh.unifiedauth.model.ProvinceResult;
import com.poisonh.unifiedauth.model.RecordModel;
import com.poisonh.unifiedauth.model.SchoolModel;
import com.poisonh.unifiedauth.model.SchoolResult;
import com.poisonh.unifiedauth.model.SupportedAppModel;
import com.poisonh.unifiedauth.model.SupportedAppResult;
import com.poisonh.unifiedauth.model.VerRecordModel;
import com.poisonh.unifiedauth.model.VerRecordResult;
import com.poisonh.unifiedauth.model.WeekDataModel;
import com.poisonh.unifiedauth.model.WeekDataResult;

import java.util.ArrayList;
import java.util.List;

import wish30.sm.sm;

/**
 * json解析工具类
 * Created by PoisonH on 2016/7/19.
 */
public class GsonUtils
{
    private static Gson mGson = new Gson();

    /**
     * 解析json数据
     *
     * @param str
     * @return
     */
    public static List<RecordModel> parseJson(String str)
    {
        List<RecordModel> mList = new ArrayList<>();
        RecordModel recordModel;
        String TIME;
        String NAME;
        String MARK;
        String ID;
        String DESCRIBE;
        String C_IMGURL;
        CertificationRecordModel certificationRecordModel = mGson.fromJson(str, CertificationRecordModel.class);
        if (certificationRecordModel.getResults().size() > 0)
        {
            for (int i = 0; i < certificationRecordModel.getResults().size(); i++)
            {
                recordModel = new RecordModel();
                TIME = certificationRecordModel.getResults().get(i).getTIME();
                NAME = certificationRecordModel.getResults().get(i).getNAME();
                MARK = certificationRecordModel.getResults().get(i).getMARK();
                ID = certificationRecordModel.getResults().get(i).getID();
                DESCRIBE = certificationRecordModel.getResults().get(i).getDESCRIBE();
                C_IMGURL = certificationRecordModel.getResults().get(i).getC_IMGURL();
                recordModel.setTIME(TIME);
                recordModel.setNAME(NAME);
                recordModel.setMARK(MARK);
                recordModel.setID(ID);
                recordModel.setDESCRIBE(DESCRIBE);
                recordModel.setC_IMGURL(C_IMGURL);
                mList.add(recordModel);
            }
        }
        return mList;
    }

    /**
     * 解析json数据
     *
     * @param str
     * @return
     */
    public static List<WeekDataModel> parseWeekData(String str)
    {
        List<WeekDataModel> mList = new ArrayList<>();

        WeekDataModel model;
        String WEEK;
        String VALUE;
        WeekDataResult mResult = mGson.fromJson(str, WeekDataResult.class);
        if (mResult.getResults().size() > 0)
        {
            for (int i = 0; i < mResult.getResults().size(); i++)
            {
                model = new WeekDataModel();
                WEEK = mResult.getResults().get(i).getWEEK();
                VALUE = mResult.getResults().get(i).getVALUE();
                model.setWEEK(WEEK);
                model.setVALUE(VALUE);
                mList.add(model);
            }
        }
        return mList;
    }

    /**
     * 解析json数据
     *
     * @param str
     * @return
     */
    public static List<PassWdModel> parsePassWdBoxData(String str, String id)
    {
        List<PassWdModel> mList = new ArrayList<>();
        byte[] key = null;
        try
        {
            key = sm.getInstance().sm3(id.getBytes(), id.getBytes().length);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        PassWdModel model;
        String TIME;
        String URL;
        String PASSWORD;
        String ACCOUNT;
        String COLLECT;
        String NAME;
        String ID;
        boolean isStart;

        PassWdResult mResult = mGson.fromJson(str, PassWdResult.class);
        if (mResult.getResults().size() > 0)
        {
            for (int i = 0; i < mResult.getResults().size(); i++)
            {
                model = new PassWdModel();

                TIME = mResult.getResults().get(i).getTIME();
                URL = mResult.getResults().get(i).getURL();
                PASSWORD = mResult.getResults().get(i).getPASSWORD().trim();
                ACCOUNT = mResult.getResults().get(i).getACCOUNT();
                COLLECT = mResult.getResults().get(i).getCOLLECT();
                NAME = mResult.getResults().get(i).getNAME();
                ID = mResult.getResults().get(i).getID();
                isStart = Integer.parseInt(COLLECT) != 0;

                try
                {
//                      String passwd = Cryptogram.decrypt(ConstantUitls.mStrPinHash.trim(), PASSWORD);

                    model.setTIME(TIME);
                    model.setURL(URL);
                    //model.setPASSWORD(Utils.encryptBASE64(sm.getInstance().sm4CbcDecrypt(PASSWORD.getBytes(), PASSWORD.getBytes().length, key.getBytes())).trim());
//                    model.setPASSWORD(new String(sm.getInstance().sm4CbcDecrypt(Utils.decryptBASE64(PASSWORD), Utils.decryptBASE64(PASSWORD).length,key)));
                    model.setPASSWORD(PASSWORD);
                    model.setACCOUNT(ACCOUNT);
                    model.setStart(isStart);
                    model.setNAME(NAME);
                    model.setID(ID);
                    mList.add(model);
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
        return mList;
    }

    /**
     * 解析消息中心json数据
     *
     * @param str
     * @return
     */
    public static List<InfoCenterModel> parseInfoCenterData(String str)
    {
        List<InfoCenterModel> mList = new ArrayList<>();

        InfoCenterModel model;
        String TIME;
        String IMGURL;
        String MESS_ID;
        String DESCRIBE;
        String TITLE;

        InfoCenterResult mResult = mGson.fromJson(str, InfoCenterResult.class);
        if (mResult.getResults().size() > 0)
        {
            for (int i = 0; i < mResult.getResults().size(); i++)
            {
                model = new InfoCenterModel();

                TIME = mResult.getResults().get(i).getTIME();
                IMGURL = mResult.getResults().get(i).getIMGURL();
                MESS_ID = mResult.getResults().get(i).getMESS_ID();
                DESCRIBE = mResult.getResults().get(i).getDESCRIBE();
                TITLE = mResult.getResults().get(i).getTITLE();

                model.setTIME(TIME);
                model.setIMGURL(IMGURL);
                model.setMESS_ID(MESS_ID);
                model.setDESCRIBE(DESCRIBE);
                model.setTITLE(TITLE);

                mList.add(model);
            }
        }
        return mList;
    }

    /**
     * 解析省份数据
     *
     * @param str
     * @return
     */
    public static List<ProvinceModel> parseProvinceData(String str)
    {
        List<ProvinceModel> mList = new ArrayList<>();

        ProvinceModel model;
        String PRO_ID;
        String PRO_NAME;
        ProvinceResult mResult = mGson.fromJson(str, ProvinceResult.class);
        if (mResult.getResults().size() > 0)
        {
            for (int i = 0; i < mResult.getResults().size(); i++)
            {
                model = new ProvinceModel();
                PRO_ID = mResult.getResults().get(i).getPRO_ID();
                PRO_NAME = mResult.getResults().get(i).getPRO_NAME();
                model.setPRO_ID(PRO_ID);
                model.setPRO_NAME(PRO_NAME);
                mList.add(model);
            }
        }
        return mList;
    }

    /**
     * 解析省份数据
     *
     * @param str
     * @return
     */
    public static List<SchoolModel> parseSchoolData(String str)
    {
        List<SchoolModel> mList = new ArrayList<>();

        SchoolModel model;
        String SCH_NAME;
        String SCH_ID;

        SchoolResult mResult = mGson.fromJson(str, SchoolResult.class);
        if (mResult.getResults().size() > 0)
        {
            for (int i = 0; i < mResult.getResults().size(); i++)
            {
                model = new SchoolModel();
                SCH_ID = mResult.getResults().get(i).getSCH_ID();
                SCH_NAME = mResult.getResults().get(i).getSCH_NAME();
                model.setSCH_ID(SCH_ID);
                model.setSCH_NAME(SCH_NAME);
                mList.add(model);
            }
        }
        return mList;
    }

    /**
     * 解析设备列表数据
     *
     * @param str
     * @return
     */
    public static List<DevicesModel> parseDeviceData(String str)
    {
        List<DevicesModel> mList = new ArrayList<>();
        DevicesModel model;
        String TIME;
        String OS;
        String PHONE_MODEL;
        String TYPE;
        DeviceResult mResult = mGson.fromJson(str, DeviceResult.class);
        if (mResult.getResults().size() > 0)
        {
            for (int i = 0; i < mResult.getResults().size(); i++)
            {
                model = new DevicesModel();

                TIME = mResult.getResults().get(i).getTIME();
                OS = mResult.getResults().get(i).getOS();
                PHONE_MODEL = mResult.getResults().get(i).getPHONE_MODEL();
                TYPE = mResult.getResults().get(i).getTYPE();

                model.setTIME(TIME);
                model.setOS(OS);
                model.setPHONE_MODEL(PHONE_MODEL);
                model.setTYPE(TYPE);

                mList.add(model);
            }
        }
        return mList;
    }

    /**
     * 解析验证记录数据
     *
     * @param str
     * @return
     */
    public static List<VerRecordModel> parseVerRecordData(String str)
    {
        List<VerRecordModel> mList = new ArrayList<>();
        VerRecordModel model;
        String TIME;//时间
      //  String HEADURL;//头像地址
        String NAME;//名字
        VerRecordResult mResult = mGson.fromJson(str, VerRecordResult.class);
        if (mResult.getResults().size() > 0)
        {
            for (int i = 0; i < mResult.getResults().size(); i++)
            {
                model = new VerRecordModel();

                TIME = mResult.getResults().get(i).gettime();
           //     HEADURL = mResult.getResults().get(i).getHEADURL();
                NAME = mResult.getResults().get(i).getname();

                model.settime(TIME);
          //      model.setHEADURL(HEADURL);
                model.setname(NAME);

                mList.add(model);
            }
        }
        return mList;
    }

    /**
     * 解析已支持应用数据
     *
     * @param str
     * @return
     */
    public static List<SupportedAppModel> parseSupportedAppData(String str)
    {
        List<SupportedAppModel> mList = new ArrayList<>();
        SupportedAppModel model;
        String TIME;
        String IMGURLFROM;
        String NAME;
        SupportedAppResult mResult = mGson.fromJson(str, SupportedAppResult.class);
        if (mResult.getResults().size() > 0)
        {
            for (int i = 0; i < mResult.getResults().size(); i++)
            {
                model = new SupportedAppModel();

                TIME = mResult.getResults().get(i).getTIME();
                IMGURLFROM = mResult.getResults().get(i).getIMGURLFROM();
                NAME = mResult.getResults().get(i).getNAME();

                model.setTIME(TIME);
                model.setIMGURLFROM(IMGURLFROM);
                model.setNAME(NAME);

                mList.add(model);
            }
        }
        return mList;
    }
}
